package com.crcc.common.exception;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;

@ApiModel(value = "返回状态标识")
public class ResultBody<T> {
    /**
     * 响应代码
     */
    @ApiModelProperty(value = "状态码")
    private String code;

    /**
     * 响应消息
     */
    @ApiModelProperty(value = "响应消息")
    private String message;

    /**
     * 响应结果
     */
    @ApiModelProperty(value = "返回数据，成功时有值")
    private T result;

    @ApiModelProperty(value = "API返回时间")
    private LocalDateTime currentDate = LocalDateTime.now();

    public ResultBody(String code,String message,T t) {
        this.code = code;
        this.message = message;
        this.result = t;
    }

    public ResultBody() {
    }

    public ResultBody(BaseResultInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }


    public LocalDateTime getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(LocalDateTime currentDate) {
        this.currentDate = currentDate;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public void setResult(T result) {
        this.result = result;
    }
    public T getResult() {
        return result;
    }

    /**
     * 成功
     *
     * @return
     */
    public static <T> ResultBody<T> success() {
        return (ResultBody<T>) success(null);
    }

    /**
     * 成功
     * @param t
     * @return
     */
    public static <T> ResultBody<T> success(T t) {
//        ResultBody<T> rb = ;
//        rb.setCode(ExceptionEnum.SUCCESS.getResultCode());
//        rb.setMessage(ExceptionEnum.SUCCESS.getResultMsg());
//        rb.setResult(t);
        return new ResultBody<T>(ExceptionEnum.SUCCESS.getResultCode(),ExceptionEnum.SUCCESS.getResultMsg(),t);
    }

    /**
     * 失败
     */
    public static <T> ResultBody<T> error(BaseResultInfoInterface errorInfo) {
        ResultBody<T> rb = new ResultBody<T>();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        rb.setResult(null);
        return rb;
    }

    /**
     * 失败
     */
    public static <T> ResultBody<T> error(String code, String message) {
        ResultBody<T> rb = new ResultBody<T>();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }


    /**
     * 失败
     */
    public static <T> ResultBody<T> error(String message) {
        ResultBody<T> rb = new ResultBody<T>();
        rb.setCode("-1");
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
